package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult page(StuQueryParam stuQueryParam) {
        PageHelper.startPage(stuQueryParam.getPage(), stuQueryParam.getPageSize());

        List<Student> studentList = studentMapper.list(stuQueryParam);

        Page<Student> p = (Page<Student>) studentList;

        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        if (ids.size() > 0){
            for (Integer id : ids) {
                if (id != null){
                    studentMapper.delete(id);
                }
            }
        }
    }

    @Override
    public void add(Student student) throws BusinessException {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        if (student.getClazzId() == null){
            throw new BusinessException("所属班级还没选~");
        }
        else{
            studentMapper.add(student);
        }

    }

    @Override
    public Student getInfo(Integer id) {
        Student student = studentMapper.getInfo(id);
        return student;

    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void violationHandle(Integer id, Integer score) {
        studentMapper.violationHandle(id, score);

    }


}
